﻿using UnityEngine;
using System.Collections;

public class SplitBehaviour : MonoBehaviour
{
	private static int m_numTotalCells = 0;
    private static int m_maxNumCells = 100;

    private static Vector3 m_startPosition = Vector3.zero;
    private float m_timeNextSplit = 0.0f;

    public Vector2 m_direction = Vector2.zero;

	// Use this for initialization
	void Start ()
	{
        //First clone
		if (m_numTotalCells == 0)
		{
            m_startPosition = this.transform.position;
		}

        m_timeNextSplit = (new System.Random().Next(1500, 2000 + 1) / 1000);
	}
	
	// Update is called once per frame
	void Update ()
	{
        m_timeNextSplit -= Time.deltaTime;
        if (m_timeNextSplit < 0.0f)
        {
            m_timeNextSplit = (new System.Random().Next(1500, 2000 + 1) / 1000) + m_timeNextSplit;

            //Room for more clones?
            if (m_numTotalCells < m_maxNumCells)
            {
                Split();
            }
        }

        this.transform.Translate(m_direction.x * Time.deltaTime, m_direction.y * Time.deltaTime, 0.0f);

        //Debug.Log(m_timeNextSplit);
		

	}

	void Split()
	{
        m_numTotalCells++;

        //Random splitting direction
        m_direction = Random.insideUnitCircle;

        //instanciate new cell.
        Transform newCellTransform = Instantiate(this.transform, this.transform.position, Quaternion.identity) as Transform;
        SplitBehaviour newCell = newCellTransform.gameObject.GetComponents<Component>()[newCellTransform.GetComponents<Component>().Length - 1] as SplitBehaviour;

        //assign new cell opposit direction.
        newCell.m_direction = -m_direction;
	}
}


/*
 * 
 * using UnityEngine;
using System.Collections;

public class SplitBehaviour : MonoBehaviour
{
	private static int m_numTotalClones = 0;
    private static Vect m_startPosition = 0;
    private int m_timeNextSplit = 0;
	private int m_maxNumClones;
    
	// Use this for initialization
	void Start ()
	{
		if (m_numTotalClones == 0)
		{
            m_startPosition = this.transform.position;
		}
	}
	
	// Update is called once per frame
	void Update ()
	{
		Clone ();
	}

	enum SPAWNDIRECTION
	{
		NORTH,
		SOUTH,
		WEST,
		EAST,
	}

	void Clone()
	{
		if (m_numClones < m_maxNumClones &&
		    m_numTotalClones < 2601)
		{
			//WaitForSeconds(1);
			//Check positions around.
			//Collect possible possitions
			System.Collections.Generic.List<SPAWNDIRECTION> possibleSpawnPoints = new System.Collections.Generic.List<SPAWNDIRECTION>();

			//Check West.
			if((m_matrixIndex % m_matrixWidth) != 0)
			{
				if(m_massMatrix[m_matrixIndex - 1] == null)
					possibleSpawnPoints.Add(SPAWNDIRECTION.WEST);
			}

			//Check East
			if((m_matrixIndex % m_matrixWidth) != m_matrixWidth-1)
			{
				if(m_massMatrix[m_matrixIndex+1] == null)
					possibleSpawnPoints.Add(SPAWNDIRECTION.EAST);
			}

			//Check North
			if((m_matrixIndex / m_matrixHeight) != 0)
			{
				if(m_massMatrix[m_matrixIndex - m_matrixHeight] == null)
					possibleSpawnPoints.Add(SPAWNDIRECTION.NORTH);
			}

			//Check South
			if((m_matrixIndex / m_matrixHeight) != m_matrixHeight-1)
			{
				if(m_massMatrix[m_matrixIndex + m_matrixHeight] == null)
					possibleSpawnPoints.Add(SPAWNDIRECTION.SOUTH);
			}

			//Randomize a spawning position.

			//If no possible spawn points where found.
			if(possibleSpawnPoints.Count == 0)
				return;
			
			Transform newCloneTransform = Instantiate (	this.transform, this.transform.position, Quaternion.identity) as Transform;
			SplitBehaviour newClone = newCloneTransform.gameObject.GetComponents<Component>()[newCloneTransform.GetComponents<Component>().Length-1] as SplitBehaviour;

			float translateDistace = 0.25f;
			System.Random spawnPoint = new System.Random();
			switch(possibleSpawnPoints[spawnPoint.Next(0, possibleSpawnPoints.Count)])
			{
			case SPAWNDIRECTION.NORTH:
				newClone.m_matrixIndex = this.m_matrixIndex - m_matrixWidth;
				newClone.transform.Translate(0,-translateDistace, 0);
				break;
			case SPAWNDIRECTION.SOUTH:
				newClone.m_matrixIndex = this.m_matrixIndex + m_matrixWidth;
				newClone.transform.Translate(0, translateDistace, 0);
				break;
			case SPAWNDIRECTION.WEST:
				newClone.m_matrixIndex = this.m_matrixIndex - 1;
				newClone.transform.Translate(-translateDistace, 0, 0);
				break;
			case SPAWNDIRECTION.EAST:
				newClone.m_matrixIndex = this.m_matrixIndex + 1;
				newClone.transform.Translate(translateDistace, 0, 0);
				break;
			}
			
			m_massMatrix[newClone.m_matrixIndex] = newClone;

			/*
			Debug.Log ("///////");
			Debug.Log ("CurrentClone_MatrixIndex = " + m_matrixIndex);
			Debug.Log ("CurrentClone_this.m_numClones = " + m_numClones.ToString());			
			Debug.Log ("CurrentClone_position = " + transform.position.x + " ; " + transform.position.y);
			Debug.Log ("CurrentClone_SplitBehaviour.m_numTotalClones = " + m_numTotalClones);			
			Debug.Log ("///////");
			//

			m_numClones++;
			m_numTotalClones++;

			/*
			Debug.Log ("///////");
			Debug.Log ("NewClone_MatrixIndex = " + newClone.MatrixIndex.ToString());
			Debug.Log ("NewClone_this.m_numClones = " + newClone.ToString());			
			Debug.Log ("NewClone_position = " + newClone.transform.position.x + " ; " + newClone.transform.position.y);
			Debug.Log ("NewClone_SplitBehaviour.m_numTotalClones = " + m_numTotalClones);			
			Debug.Log ("///////");
			/
		}
	}
}
*/
